import api from '../../api/api'
import * as type from '../mutationType'

const state = {
    list: []
};

const actions = {
    getList({commit}, condition) {
        commit(type.SET_LOADING_STATE, true);
        api.getList(condition, (res)=>{
            commit(type.SET_LIST_DATA, res);
            commit(type.SET_LOADING_STATE, false);
        })
    },
    getFilter({commit}, name) {
        commit(type.GET_FILTER_DATA, name);
    }
};

const getters = {
    getList: state => state.list
};

const mutations = {
    [type.SET_LIST_DATA](state, res){
        state.list = res
    },
    [type.GET_FILTER_DATA](state, name) {
        state.list.list = state.list.list.filter((item)=>{
           return item.name.indexOf(name) >=0 
        })
    },
    [type.ADD_TO_CART](state, res) {
       
    },
    [type.REMOVE_TO_CART](state, id) {
        var record = state.list.list.find(item => item.goodsId === id);
        record.inventory++;
    },
};

export default {
    state,
    actions,
    getters,
    mutations
}
